/*
 *  Copyright (c) 2010 Sonrisa Informatikai Kft. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Sonrisa Informatikai Kft. ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Sonrisa.
 *
 * SONRISA MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SONRISA SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */
package com.zh.jbet.web.components.datatable;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import org.apache.wicket.Component;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.ChoiceFilteredPropertyColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterForm;
import org.apache.wicket.markup.html.basic.EnumLabel;
import org.apache.wicket.markup.html.form.EnumChoiceRenderer;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

/**
 * Egy enum értékeit megjelenítő oszlop, ami az Enum lehetséges értékei által
 * filterezhető is.
 * @param <T>
 * @param <Y> 
 * @author golyo
 */
public class EnumChoiceFilteredPropertyColumn<T extends Enum<T>> extends ChoiceFilteredPropertyColumn<T, T> {

    private final IChoiceRenderer<T> choiceRenderer = new EnumChoiceRenderer<T>();

    public EnumChoiceFilteredPropertyColumn(IModel<String> displayModel, String sortProperty, String propertyExpression, Class<T> enumChoice) {
        super(displayModel, sortProperty, propertyExpression, new Model((Serializable)Arrays.asList(enumChoice.getEnumConstants())));
    }

    /**
     *
     * @param displayModel
     * @param sortProperty
     * @param propertyExpression
     * @param enumValues a szűrő legördülőben lévő elemek listája
     */
    public EnumChoiceFilteredPropertyColumn(IModel<String> displayModel, String sortProperty, String propertyExpression, List<T> enumValues) {
        super(displayModel, sortProperty, propertyExpression, new Model((Serializable)enumValues));
    }


    public EnumChoiceFilteredPropertyColumn(IModel<String> displayModel, String propertyExpression, Class<T> enumChoice) {
        this(displayModel, null, propertyExpression, enumChoice);
    }

    @Override
    public void populateItem(Item<ICellPopulator<T>> item, String componentId, IModel<T> rowModel) {
        item.add(new EnumLabel(componentId, createLabelModel(rowModel)));
    }


    @Override
    protected IChoiceRenderer<T> getChoiceRenderer() {
        return choiceRenderer;
    }

    @Override
    public Component getFilter(String componentId, FilterForm form) {
        Component filter = super.getFilter(componentId, form);
        String clz = getPropertyExpression() + "Choice";
        filter.add(new AttributeAppender("class", new Model<String>(clz), " "));
        return filter;
    }
}
